--[[ Copyright 2012, Chris Ho                               ]]--
--[[ MIT License (Please read the LICENSE file for details) ]]--

-- Crinkle's (Mercenary) Persona
-- Conventions: always end statements with semicolon
-- Global variables are all uppercase
-- Functions are CamelCase, with first letter capitalized
--
-- To use this as a Homunculus AI, put it in USER_AI and type "/hoai"
-- To use this as a Mercenary AI, put it in USER_AI and type "/merai"
-- To enable file logging, type "/traceai" (to "TraceAI.txt" in iRO install path)

-- -- -- -- -- -- -- --

-- Base Path of Script Components (Relative):
ROOT = "./";
PATH = ROOT .. "AI/USER_AI/";

--[[ Load and Execute (protected dofile()) ]]--
function Include(_filename)
  TraceAI(_filename .. ": " .. tostring(pcall(loadfile(PATH .. _filename))));
end
--[[ Qualify a Module Name (for use in require()) ]]--
function Qualify(_name)
  return PATH .. _name .. ".lua";
-- TODO: for Lua v5.1 compatibility
--  return PATH .. _name;
end
--[[ Version-Independent Module Load ]]--
function Use(_name)
  require(PATH .. _name .. ".lua");
-- TODO: for Lua v5.1 compatibility
--  require(PATH .. _name);
end

-- TODO: for Lua v5.1 compatibility
--package.path = "./AI/USER_AI/?.lua;" .. package.path;

-- Load the core package (common to both Homunculus and Mercenary)
Use("Core");

-- Tranquility determines how quickly the pet escalates its aggression.
-- It mainly determines skill usage under fire (danger tolerance).
-- (Must be at least 1!) Higher values men more relaxed behavior in battle.
--
-- The base tranquility may not be the same as the adjusted personality
-- value.
TRANQUILITY = 6.0;

-- Resilience rates how hard the pet tries to do its job. It mainly determines
-- task abort/behavior change. (Must be at least 1!) Higher values mean
-- more perseverence.
--
-- The base resilience may not be the same as the adjusted personality
-- value.
RESILIENCE = 2.0;

-- Maximum Distance from Owner
LEASH = 15;

-- Maximum Threat Distance
VISIBILITY = 20;

-- Friends File
FRIENDS_FILE = PATH .. "friends.txt";

-- Default Personality Files
PERSONALITY_ARCHER = PATH .. "archer/Archer.lua";
PERSONALITY_LANCER = PATH .. "lancer/Lancer.lua";
PERSONALITY_SWORDMAN = PATH .. "fencer/Fencer.lua";

-- Map of Mercenary Types to Personality File Names (Relative):
MERCENARIES = {};
MERCENARIES[LANCER01] = PATH .. "lancer/l01.lua";
MERCENARIES[LANCER02] = PATH .. "lancer/l02.lua";
MERCENARIES[LANCER03] = PATH .. "lancer/l03.lua";
MERCENARIES[LANCER04] = PATH .. "lancer/l04.lua";
MERCENARIES[LANCER05] = PATH .. "lancer/l05.lua";
MERCENARIES[LANCER06] = PATH .. "lancer/l06.lua";
MERCENARIES[LANCER07] = PATH .. "lancer/l07.lua";
MERCENARIES[LANCER08] = PATH .. "lancer/l08.lua";
MERCENARIES[LANCER09] = PATH .. "lancer/l09.lua";
MERCENARIES[LANCER10] = PATH .. "lancer/l10.lua";
MERCENARIES[ARCHER01] = PATH .. "archer/a01.lua";
MERCENARIES[ARCHER02] = PATH .. "archer/a02.lua";
MERCENARIES[ARCHER03] = PATH .. "archer/a03.lua";
MERCENARIES[ARCHER04] = PATH .. "archer/a04.lua";
MERCENARIES[ARCHER05] = PATH .. "archer/a05.lua";
MERCENARIES[ARCHER06] = PATH .. "archer/a06.lua";
MERCENARIES[ARCHER07] = PATH .. "archer/a07.lua";
MERCENARIES[ARCHER08] = PATH .. "archer/a08.lua";
MERCENARIES[ARCHER09] = PATH .. "archer/a09.lua";
MERCENARIES[ARCHER10] = PATH .. "archer/a10.lua";
MERCENARIES[SWORDMAN01] = PATH .. "fencer/f01.lua";
MERCENARIES[SWORDMAN02] = PATH .. "fencer/f02.lua";
MERCENARIES[SWORDMAN03] = PATH .. "fencer/f03.lua";
MERCENARIES[SWORDMAN04] = PATH .. "fencer/f04.lua";
MERCENARIES[SWORDMAN05] = PATH .. "fencer/f05.lua";
MERCENARIES[SWORDMAN06] = PATH .. "fencer/f06.lua";
MERCENARIES[SWORDMAN07] = PATH .. "fencer/f07.lua";
MERCENARIES[SWORDMAN08] = PATH .. "fencer/f08.lua";
MERCENARIES[SWORDMAN09] = PATH .. "fencer/f09.lua";
MERCENARIES[SWORDMAN10] = PATH .. "fencer/f10.lua";



--[[ Main AI Entrypoint ]]--
function AI(_id)
  -- If the ID changes (or is not set), then initialize the pet.
  if ((ME == nil) or (ME.id ~= _id)) then
    -- Determine the personality file.
    local filename = PERSONALITY_DEFAULT;
    local category = GetV(V_MERTYPE, _id);
    if (MERCENARIES[category]) then filename = MERCENARIES[category] end

    -- Start the pet AI.
    Initialize(_id, filename);
    Info("AI() Mercenary type [" .. category .. "] is " .. ME.id .. ", Owner is " .. ME.owner.id);
  end

  -- If we're idle here, then start planning new tasks.
  if (PHASE == PHASE_IDLE) then PHASE = PHASE_PLAN end

  -- Step 1: Read incoming commands.
  Survey();
  -- Step 2: Decide on a task.
  Decide();
  -- Step 3: Process the resulting actions based on task.
  Act();
  -- Step 4: React to the environment.
  Reflect();
end
